/*******************    (C) COPYRIGHT SUNSHINE SILICON    **********************
* File Name: main.c
* Description:

    HS6200 Demo Aerocraft
    
*  Copyright (C) 2014-2035 Sunshine Silicon Corporation (www.sunsili.com), All Rights Reserved
*  SPDX-License-Identifier: Apache-2.0
*
*  Licensed under the Apache License, Version 2.0 (the "License"); you may
*  not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
*  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*
*  This file is part of HS6200 SDK                                                                                                                             
 *                                                                                                                                  
 *                                                                                                                                  
 *                                    ___________________________                           \ /    
 *                                   |                           |        _____________      |                             
 *                                   |                           |       |             |     |                                  
 *                                   |                     P0.0  | --->  |  nCE        |-----|                                     
 *                                   |                S  | P0.1  | --->  |  SCK | S    |   H                                     
 *                                   |                P  | P0.2  | <---  |  MISO| P    |   S                                     
 *                                   |                I  | P0.3  | --->  |  MOSI| I    |   6  (as PRX)                     
 *                                   |                   | P0.7  | --->  |  nSS |      |   2                                     
 *             /-------------------- | VBUS                P0.6  | <---  |  IRQ        |   0                                     
 *      P ____/________USB__________ | D+                        |       |_____________|   0                                     
 *      C     \        Bus           | D-                        |                                               
 *             \-------------------- | GND                       |                                         
 *                                   |                           |                                     
 *                                   |                           |                                                     
 *                                   |                           |                                           
 *                                   |                           |                                                              
 *                                   |                     P1.0  | ---> SYSCLK Output,used for test.                            
 *                                   |                           |                                                                  
 *                                   |                           |                                                               
 *                                   |                     P2.2  | ---> LED1 ( LED On when P2.2 output 0 )                       
 *                                   |                     P2.3  | ---> LED2 ( LED On when P2.3 output 0 )                       
 *                                   |                           |                                                               
 *                                   |___________________________|                                                                   
 *                                                                                                                                 
 *                                             C8051F380                                                                                        
 *                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
 */        
 
#include "init_device.h" 
#include "C8051F380_defs.h"

#include "stdio.h"
#include "stdlib.h"
#include "C8051F38X_clock.h"
#include "C8051F_Delay.h"


#include "HS6200.h"
#include "Debug.h"

#include "Hw_timer.h"
#include "hw_uart.h"


#define SYSCLK      48000000           // SYSCLK frequency in Hz
#define SCAN_ACK_VALUE	0xa5
#define DATA_ACK_VALUE  0x5a

#define MAX_RT    0x10
#define TX_DS   	0x20
#define RX_DR   	0x40

#define OUT_LINE_CH 80
#define Remoter_Tx_Payload_Length 10
#define LOST_COUTER_MAX 200

typedef enum
{
	ON_LINK,
	OUT_LINK
}LindStatusT;
LindStatusT Linkstatus;

typedef enum
{
	START,
	STOP,
}HoppingFlagT;

typedef struct
{
	HoppingFlagT Hopping_Start_Flag;
	U8 Hopping_Faster_Flag;
	U8 RF_Freq_Hop_Channal_Idx;
	U8 HoppingWait;
	U8 HoppingCounter;
	U8 LostCounter;
}ChannelCtlT;
ChannelCtlT ChannelCtl;

typedef enum
{
	ADDR_OUT_LINK,
	ADDR_ON_LINK
}AddrT;
AddrT Current_Addr_Flag;

typedef enum
{
	Msg_T0,
	Msg_T1,
	Msg_T2,
	Msg_T3,
}MsgT;
MsgT RecMsgType;

typedef struct
{
	MsgT MsgType;
	U8 MsgTypeIndex;
}MsgTypeCtlT;
MsgTypeCtlT MsgTypeCtl;

typedef struct
{
	U8 timeout_flag;
	U8 timeWait;

}TimeSlotCtlT;
TimeSlotCtlT TimeSlotCtl;

U8 SPI_Flag=SPI_FLG_IDLE;

U8 HS6200_Board_Addr[5]= {0x55,0x42,0x9C,0x8F,0xC9};                //default address
U8 RF_Freq_Hop_Channal_on_link[5]= {0x55,0x83,0x2A,0x66,0x4B};                //default address

U8 RF_Freq_Hop_Channal[]={80, 22,43, 67, 77};

S8 HS6200_Tx_Payload[32];
U8 HS6200_Rx_Payload[32];

U8 slot_conter;


void HS_ASSERT(U8 c,U8 assert_ID)
{
	U8 assertID = 0;
	assertID = assert_ID;
	
	if (!c)
	{
		EA = 0;
		
		LED1_ON;
		LED2_ON;
		
		while(1);
	}
}

void ChannelIndexPlus(void)
{
	ChannelCtl.RF_Freq_Hop_Channal_Idx++; 
	if(ChannelCtl.RF_Freq_Hop_Channal_Idx >= sizeof(RF_Freq_Hop_Channal))
	{
		ChannelCtl.RF_Freq_Hop_Channal_Idx = 0; 
				//LED_TIME_DEBUG_TRIG;
	}
	LED_TIME_DEBUG3_TRIG;
}

void MsgTypeIndexPlus(void)
{
	MsgTypeCtl.MsgTypeIndex++; 
	if(MsgTypeCtl.MsgTypeIndex >= 4)
	{
		MsgTypeCtl.MsgTypeIndex = 1; /*M0 used for out link mode*/
	} 
	//LED_TIME_DEBUG1_TRIG;
}


void ParamInit()
{
	U8 i;
	
	Linkstatus = ON_LINK;
	
	ChannelCtl.Hopping_Start_Flag = STOP;
	ChannelCtl.RF_Freq_Hop_Channal_Idx = 0;
	ChannelCtl.HoppingWait = 0;
	ChannelCtl.HoppingCounter = 0;
	ChannelCtl.LostCounter = 0;
	ChannelCtl.Hopping_Faster_Flag = 0;

	Current_Addr_Flag = ADDR_OUT_LINK;

	TimeSlotCtl.timeWait = 0;
	TimeSlotCtl.timeout_flag = 0;
	slot_conter = 0;

	
	MsgTypeCtl.MsgTypeIndex = Msg_T0;

	for(i=0x00;i<32;i++)
		HS6200_Tx_Payload[i]=i;
	HS6200_Tx_Payload[9] = Msg_T0;

	LED_TIME_DEBUG1_OFF;
	LED_TIME_DEBUG_OFF;
	
}



void On_Link_Thread(void)
{
	U8 i;
	while(0 == TimeSlotCtl.timeout_flag);
	TimeSlotCtl.timeout_flag = 0; 

	for (i=0;i<10;i++)
		HS6200_Rx_Payload[i] = 0x00;
	
	printf("s=%b.2x\r\n",slot_conter);
	
	if (HS6200_RecivePack(HS6200_Rx_Payload))
	{
		ChannelCtl.LostCounter = 0;
	
		/*process rec msg*/
		switch(HS6200_Rx_Payload[9])
		{
			/*remoter had reseted power*/
			case Msg_T0:
				HS6200_ChangeAddr_Reg(RF_Freq_Hop_Channal_on_link,5);  					
				ChannelCtl.Hopping_Start_Flag = STOP;
				ChannelCtl.Hopping_Faster_Flag = 0;
				LED_TIME_DEBUG_TRIG;
				LED2_ON;
				LED1_OFF;
				break;
				
			case Msg_T1:
				ChannelCtl.Hopping_Start_Flag = START;
				ChannelCtl.Hopping_Faster_Flag = 0;
				ChannelCtl.HoppingWait = 25; /*26ms*/
				LED_TIME_DEBUG_TRIG;
				LED1_OFF;
				break;
				
			case Msg_T2:
				ChannelCtl.Hopping_Start_Flag = START;
				ChannelCtl.Hopping_Faster_Flag = 0;
				ChannelCtl.HoppingWait = 15; /*16ms*/
				LED_TIME_DEBUG1_TRIG;
				LED1_OFF;
				break;
				
			case Msg_T3:
				ChannelCtl.Hopping_Start_Flag = START;
				ChannelCtl.Hopping_Faster_Flag = 0;
				ChannelCtl.HoppingWait = 5; /*6ms*/
				LED_TIME_DEBUG2_TRIG;
				LED1_OFF;
				break;
				
			default:
				//HS_ASSERT(0,2);
				break;
		}
	}

	/*execute hopping or not*/
	if (START == ChannelCtl.Hopping_Start_Flag)
	{
		if (0 == ChannelCtl.HoppingWait)
		{
			ChannelIndexPlus();
			HS6200_Change_CH(RF_Freq_Hop_Channal[ChannelCtl.RF_Freq_Hop_Channal_Idx]);
			if (1 == ChannelCtl.Hopping_Faster_Flag)
				ChannelCtl.HoppingWait = 10; /*10ms*/
			else
				ChannelCtl.HoppingWait = 29; /*30ms*/
		}
		else 
		{
			ChannelCtl.HoppingWait--;
		}
	
		/*reconnect control*/
		ChannelCtl.LostCounter++;
		/*if miss connecting time long than max time,the aerocraft should hopping faster*/
		if ((LOST_COUTER_MAX == ChannelCtl.LostCounter)&&(0 == ChannelCtl.Hopping_Faster_Flag))
		{
			ChannelCtl.LostCounter = 0;
			ChannelCtl.Hopping_Faster_Flag = 1;
			LED1_ON;
		}

	}

}


void main(void)
{ 	

	SysClkInit();  
    
    Init_Device();
	
	Delay_ms(100);

	HS6200_CE_Low();
 	HS6200_Init();
	HS6200_CE_High(); 
	
	ParamInit();
		
	Timer0_InitMs();

	HS6200_Change_CH(OUT_LINE_CH);	

	printf("WWW.HUNTERSUN.COM\r\n");
	
	LED1_ON;
	
	while(1)
	{
		On_Link_Thread();
	}   

}

/*-----------------------SPI ISR-------------------------*/
void SPI_Isr(void) interrupt 6// SPI interrupt
{
	if(SPIF==0x01)	   //SPI interrupt flag
	{
		SPI_Flag=SPI_FLG_IDLE;
		SPIF=0x00;  //clear									
	}
	if(RXOVRN==0x01)   //rx overflow
	{
		
	}
}

/*-----------------------Timer0 ISR-------------------------*/
void Timer0_ISR (void) interrupt 1 using 3
{
	TImer0Disable();
	Timer0_InitMs(); 
	TimeSlotCtl.timeout_flag = 1;

	slot_conter++;
	if (255 ==slot_conter)
	{
		slot_conter = 0;
	}
	
	
}
/*---------------------------------------End Of File----------------------------------------*/

